package com.gzp.service.impl;

import java.util.Arrays;
import java.util.List;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import com.gzp.pojo.UserAddress;
import com.gzp.service.OrderService;
import com.gzp.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

/**
 * springBoot使用注解org.apache.dubbo.config.annotation.Reference 接收提供者的服务
 * 
 * @author gzp
 *
 */
@Service
public class OrderServiceImpl implements OrderService {

	@Reference
	private UserService userService;

	@Override
	@HystrixCommand(fallbackMethod = "fallbackInitOrder") // 熔断器服务降级注解
	public List<UserAddress> initOrder(String userId) {
		if ("1".equals(userId)) {
			throw new RuntimeException("网络异常");
		}
		return userService.findAllAddress(userId);
	}

	/**
	 * initOrder的降级方法
	 * 
	 * @param userId
	 * @return
	 */
	public List<UserAddress> fallbackInitOrder(String userId) {
		return Arrays.asList(new UserAddress(userId, "网络拥挤", userId));
	}

}
